<?php
declare (strict_types=1);

namespace app\backend_api\validate;

use app\common\validate\BaseValidate;
use think\Validate;

class AdminValidate extends BaseValidate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'username' => 'require|min:3|max:18|unique:admin_users,name',
        'email' => 'require|email|unique:admin_users', // 这里的字段名与模型属性名相同
        'password' => 'require|min:3|max:10',
        'checkPass' => 'require|min:3|max:10|confirm:password',
        'mobile' => 'require|regex:/^\d{11}$/',
        'role_id' => 'require|array',
        'avatar' => 'requireIf:avatar,true|max:500', // 需要有值且长度不超过 500
        'status' => 'boolean'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [];

    protected function sceneEdit()
    {
        return $this->remove('username', 'unique')
            ->remove('email', 'unique')
            ->remove('password', 'require')
            ->append('password', 'requireIf:password,true')
            ->remove('checkPass', 'require')
            ->append('checkPass', 'requireIf:checkPass,true');
    }
}
